<template>
	<view class="case-list">
		<delVue :Visible="isshow" @isModal="modal" @goStay="stay" :oneText="one" :twoText="two" :threeText="three"
			:fourText="four" />
		<view class="title">
			<alltitleVue :title="title" :url="url" />
		</view>

		<view class="main-content">
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" show-scrollbar="true">
				<block v-for="(item, index) in ready_list" :key="index">
					<view class="case-card">
						<view class="case-title">
							<view class="left">{{ item.name }}</view>
							<view class="title-right">
								<view @click.stop="detailHandle(item)" class="right-text">查看详情</view>
								<text>|</text>
								<u-icon size="32" name="trash" color="#CCA566" @click.stop="deleteItem(item)"></u-icon>

							</view>
						</view>
						<hr class="case-hr" />
						<view class="case-content" @click.stop="seleckCase(item)">
							<view class="case-content-left">
								<view class="case-complaint">
									<view class="left">年龄：</view>
									<view class="right">{{ calculateAge(item.idCard) }} 岁</view>
								</view>
								<view class="case-complaint">
									<view class="left">手机号：</view>
									<view class="right">{{ item.mobile }}</view>
								</view>
								<view class="case-complaint">
									<view class="left">证件号：</view>
									<view class="right">{{ item.idCard }}</view>
								</view>
								<view class="case-complaint">
									<view class="left">创建时间：</view>
									<view class="right">{{ item.createTime }}</view>
								</view>
							</view>
						</view>
					</view>
				</block>
			</scroll-view>
			<view v-if="isMore" class="text-center text-grey text-sm bottom">暂无更多数据</view>
		</view>
		<view class="foot padding-lr">
			<button class="cu-btn btn block lg btn-bg" @click="add_case">添加就诊人</button>
		</view>

	</view>
</template>

<script>
	import delVue from '../../components/del.vue'
	import {
		toast,
		getStore,
		backPrevPage
	} from "@/utils";
	import {
		patient,


		patientDelete
	} from "@/service/api.js";

	export default {
		components: {
			delVue
		},
		data() {
			return {
				isshow: false,
				one: "再想想",
				two: "确定删除",
				three: "确定要删除此就诊人吗？",
				four: "删除提示",



				title: '就诊卡管理',
				originModule: "",
				modalName: "",
				modelType: "",
				addType: "", //添加就诊人类型
				ready_list: [],
				// patientAdd: require("./static/patientAdd_img.png"),
				page: {
					size: 20,
					pageNo: 1
				},

				id: "",
				total: "",
				isMore: false,
				tipModel: "",
				payment: "",
				nucleicAcid: "",
				safeArea: getApp().globalData.safeArea,
				qrcode: "",
				url: "",
			};
		},
		onLoad(options) {
			if (options.url) {
				this.url = options.url
			}

		},
		onShow() {
			this.getPatient();
		},
		methods: {

			modal(item) {
				this.isshow = item;
			},
			stay(item) {
				this.isshow = item;
				this.deleteHandle();
			},

			deleteItem(item) {
				this.id = item.id
				this.isshow = true
			},
			calculateAge(idCard) {

				if (!idCard) {
					return "无效身份证";
				}

				// 提取出生日期部分
				const birthDate = idCard.substr(6, 8); // YYYYMMDD

				const birthYear = parseInt(birthDate.substr(0, 4));
				const birthMonth = parseInt(birthDate.substr(4, 2)) - 1; // 月份从0开始
				const birthDay = parseInt(birthDate.substr(6, 2));

				// 转换为 Date 对象
				const birth = new Date(birthYear, birthMonth, birthDay);

				// 获取当前日期
				const now = new Date();

				// 计算年龄
				let age = now.getFullYear() - birth.getFullYear();

				// 判断当前日期是否已经过了生日
				const isBirthdayPassed = (now.getMonth() > birth.getMonth()) ||
					(now.getMonth() === birth.getMonth() && now.getDate() >= birth.getDate());

				if (!isBirthdayPassed) {
					age--; // 如果生日还没到，减去1岁
				}

				return age;
			},
			async deleteHandle() {

				const res = await patientDelete(this.id);
				if (res.code == 200) {
					toast("删除成功");

					this.getPatient();
				}
			},



			seleckCase(item) {
				if (this.url != '') {
					uni.navigateBack({
						delta: 1,
					});
				} else {
					uni.navigateTo({
						url: `/My/addCard/index?item=${JSON.stringify(item)}`
					});
				}

			},



			add_case() {
				uni.navigateTo({
					url: "/My/addCard/index"
				})
			},


			detailHandle(item) {
				uni.navigateTo({
					url: `/My/addCard/index?item=${JSON.stringify(item)}`
				});
			},
			//*获取订单列表*/
			async getPatient() {


				let res = await patient();
				console.log(res, "444444")
				if (res.code == 200) {

					this.ready_list = res.rows;
				}

			}
		}
	};
</script>

<style lang="scss" scoped>
	.case-list {
		background: #F5F3F0;
		height: 100vh;
		display: flex;
		flex-direction: column;
		overflow: hidden;

		.main-content {
			flex: 1;
			height: 100%;
			overflow: hidden;
			overflow-y: scroll;
			padding: 24rpx;
		}

		.bg-blue {
			background-color: #fff !important;
		}

		.bottom {
			margin-bottom: 20rpx;
		}

		.case-card {
			min-height: 300rpx;
			background: #fff;
			border-radius: 8rpx;
			margin-bottom: 24rpx;

			.case-title {
				padding: 24rpx 24rpx 0 40rpx;
				height: 60rpx;
				display: flex;
				justify-content: space-between;
				line-height: 60rpx;

				.left {
					min-width: 50%;
					font-size: 34rpx;
					font-weight: 600;
				}

				.title-right {
					color: #CCA566;
					display: flex;
					align-items: center;
					width: 180rpx;
					justify-content: space-between;

					.right-text {
						font-size: 28rpx;
						color: #CCA566;
					}

					text {
						font-size: 22rpx;
						color: #E6E6E6;
					}
				}
			}

			.case-hr {
				height: 1px;
				background: #eee;
				width: 98%;
				margin-left: 1%;
				margin-top: 20rpx;
			}

			.case-content {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 24rpx 0rpx;

				.case-content-left {
					width: 70%;
				}

				.case-content-right {
					width: 30%;
					text-align: center;

					image {
						width: 100rpx;
						height: 100rpx;
					}
				}
			}

			.case-complaint {
				height: 50rpx;
				display: flex;
				padding: 0 0 0 40rpx;
				line-height: 60rpx;
				font-size: 28rpx;

				.right {
					max-width: 80%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
		}
	}

	.foot {
		position: fixed;
		bottom: 0;
		height: 140rpx;
		z-index: 100;
		width: 100%;
		border-radius: 0;
		background: #fff;
		padding-top: 20rpx;

		.btn-bg {
			width: 702rpx;
			background: #CCA566;
			color: #fff;
		}



		.margin-top {
			margin-top: 100upx;
		}
	}

	.bg {
		width: 100%;
		height: 120rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 24rpx;
		padding-top: 65rpx;


		.title-img {
			width: 100rpx;
		}

		text {
			display: inline-block;

			color: #1a1a1a;
			font-size: 32rpx;
			font-weight: bold;
		}

		view {
			width: 100rpx;
		}
	}

	.bg-color {
		// background: #fff;
	}

	.title {
		background: #fff;
	}
	.scroll-Y{
		height: calc(100vh - 391rpx);
	}
</style>